<template>
  <a-drawer v-model:visible="LAYER.visible" :title="LAYER.title" :width="LAYER.width"
            :closable="LAYER.closable" :mask-closable="LAYER.maskClosable" :esc-to-close="LAYER.escToClose"
            :unmount-on-close="LAYER.unmountOnClose" :footer="LAYER.footer" :ok-text="LAYER.okText"
            :ok-loading="LAYER.okLoading" :cancel-text="LAYER.cancelText" :on-before-ok="onSubmit" @close="onCancel">
    <a-form ref="formRef" :model="formState" layout="vertical" :rules="RULES">
      <a-row :gutter="12">

        <a-col :span="24">
          <a-form-item label="活动标题" field="title">
            <a-input v-model="formState.title" allow-clear placeholder="请填注册标题"/>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="活动说明(仅后端可见)" field="description">
            <a-textarea v-model="formState.description"/>
          </a-form-item>
        </a-col>

        <a-col :span="24">
          <a-form-item label="活动性质" field="nature">
            <smart-dict v-model="formState.nature" is-radio :url="DictOptions.HdNature" placeholder="请设置活动性质"/>
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="活动状态" field="state">
            <smart-dict v-model="formState.state" is-radio :url="DictOptions.HdState" placeholder="请设置活动状态"/>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="活动分类" field="kind">
            <smart-dict v-model="formState.kind" is-radio :url="DictOptions.RegisterKind" placeholder="请设置活动分类"/>
          </a-form-item>
        </a-col>



        <a-col :span="12">
          <a-form-item label="推广海报" field="posterId">
            <smart-dict v-model="formState.posterId" :url="PopOptions.PosterOptions" placeholder="请选择推广海报"/>
          </a-form-item>
        </a-col>

        <a-col :span="12">
          <a-form-item label="注册链接" field="linkId">
            <smart-dict v-model="formState.linkId" :url="PopOptions.LinkOptions" placeholder="请选择关联链接"/>
          </a-form-item>
        </a-col>

        <a-col  :span="24">
          <a-form-item label="奖励规则" field="reward.rule" :rules="RULES.reward.rule">
            <smart-dict v-model="formState.reward.rule" :url="DictOptions.RewardRule" placeholder="请选择活动奖励规则" @change="onChangeRule"/>
          </a-form-item>
        </a-col>


        <a-col   :span="24">
          <a-divider />
          <a-form-item label="规则参数设置">
            <template #help>
              <a-typography-text style="padding-top: 24px" type="secondary">奖励次数设置为0时,系统不控制奖励发放次数,当用户满足奖品发放条件时间,系统自动发放</a-typography-text>
            </template>
            <a-space>
              <span>每成功邀请</span>
              <a-input-number v-model="formState.reward.achievedQuantity" style="width: 80px" :min="1"/>
              <span>个新用户注册,奖励</span>
              <a-input-number v-model="formState.reward.rewardQuantity" style="width: 80px" :min="1"/>
              <span>份活动奖励 , 最多可以获取</span>
              <a-input-number v-model="formState.reward.mostQuantity" style="width: 80px"/>
              <span>份活动奖励</span>
            </a-space>
          </a-form-item>
        </a-col>


      </a-row>
    </a-form>
    <!--表单结束-->
  </a-drawer>
</template>

<script lang="ts" setup>


import {EditDrawer} from '@/libs/core'

import {REGISTER_EDIT} from "../resource/form";
import {PopActivityHttp} from '../resource/http'
import {DictOptions, PopOptions} from "@/libs/options";



const props = defineProps({record: {type: Object, required: true}})
const emit = defineEmits(['cancel'])
const {BODY, RULES} = REGISTER_EDIT

const {addActivity, updateActivity} = PopActivityHttp
const config = {tplRecord: BODY, rawRecord: props.record, rowKey: 'id', emit}


const service = new EditDrawer(config).setAddRequest(addActivity).setUpdateRequest(updateActivity)
const {formRef, formState, LAYER, isUpdate, onCancel, onSubmit} = service
LAYER.title = isUpdate ? '修改注册' : '新建注册'

const onChangeRule = () => {
  const reward = formState.value.reward
  reward.achievedQuantity = 0
  reward.rewardQuantity = 0
}


if (isUpdate) formState.value.reward.rule = props.record.reward.rule.value

</script>
